import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter_ytx_plugin/src/utils/screen_utils.dart';

import '../view/video_view.dart';

extension WidgettExt on Widget{
  static Widget bottomServiceAndSub(BuildContext context,
      {required String asset,
      String subText = "保存",
      Function? onAsset,
      Function? onSubmit}) {
    var bottom = MediaQuery.of(context).padding.bottom;
    return Container(
      height: 83.wpx,
      padding: EdgeInsets.only(
          left: 16.wpx,
          right: 16.wpx,
          top: bottom == 0 ? 11.wpx : 0,
          bottom: bottom == 0 ? 11.wpx : bottom / 2),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          top: BorderSide(
            color: const Color.fromRGBO(0, 0, 0, 0.05),
            width: 1.hpx,
          ),
        ),
      ),
      child: Row(
        children: [
          GestureDetector(
            onTap: () {
              onAsset?.call();
            },
            child: Image.asset(
              asset,
              width: 24.wpx,
              height: 24.wpx,
              color: Colors.black,
            ),
          ),
          SizedBox(
            width: 20.wpx,
          ),
          GestureDetector(
            onTap: () {
              onSubmit?.call();
            },
            child: Container(
              width: 375.wpx - 32.wpx - 44.wpx,
              height: 40.wpx,
              decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.all(Radius.circular(4.wpx))),
              child: Center(
                child: Text(
                  '$subText',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.tpx,
                    fontWeight:
                        Platform.isIOS ? FontWeight.w600 : FontWeight.w500,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  static void showBottomSheet(
      BuildContext context, List<String> list, Function function) {
    showModalBottomSheet(
        context: context,
        backgroundColor: Colors.white,
        builder: (BuildContext context) {
          int position = -1;
          return Container(
            height: MediaQuery.of(context).size.height * 0.3, // 设置自定义高度
            child: Column(
              children: [
                Expanded(
                    child: ListView.builder(
                        itemBuilder: (ctx, index) {
                          return GestureDetector(
                            onTap: () {
                              position = index;
                              Navigator.pop(context);
                              function.call(index);
                            },
                            child: Container(
                              height: 60.hpx,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  border: Border(
                                    top: BorderSide(
                                      color:
                                          const Color.fromRGBO(0, 0, 0, 0.05),
                                      width: 1.hpx,
                                    ),
                                  )),
                              child: Text(
                                list[index],
                                style: TextStyle(fontSize: 16.tpx),
                              ),
                            ),
                          );
                        },
                        itemCount: list.length)),
              ],
            ),
          );
        });
  }

  static  Widget bottomSubAny({
    required List<Map<String, dynamic>> buttons,
    required BuildContext context,
  }) {
    var bottom = MediaQuery.of(context).padding.bottom;
    return Container(
      width: 375.wpx,
      height: 83.wpx,
      padding: EdgeInsets.only(
          left: 16.wpx,
          right: 16.wpx,
          top: bottom == 0 ? 11.wpx : 0,
          bottom: bottom == 0 ? 11.wpx : bottom / 2),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          top: BorderSide(
            color: const Color.fromRGBO(0, 0, 0, 0.05),
            width: 1.hpx,
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: buttons.map<Widget>((button) {
          return Expanded(
            child: GestureDetector(
              onTap: () {
                button['function']?.call();
              },
              child: Container(
                height: 40.wpx,
                margin: EdgeInsets.symmetric(horizontal: 7.5.wpx), // 15 / 2
                decoration: BoxDecoration(
                  color: button['color'] ?? Colors.black,
                  // 使用自定义颜色，如果没有提供就使用默认黑色
                  borderRadius: BorderRadius.all(Radius.circular(4.wpx)),
                ),
                child: Center(
                  child: Text(
                    '${button['text']}',
                    style: button['textStyle'] ??
                        TextStyle(
                          color: Colors.white,
                          fontSize: 14.tpx,
                          fontWeight: Platform.isIOS
                              ? FontWeight.w600
                              : FontWeight.w500,
                        ),
                  ),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

  static  Future<void> confirmDelete(BuildContext context, Function? function,
      {String? content = ''}) async {
    showCupertinoDialog(
      context: context,
      builder: (context) {
        return CupertinoAlertDialog(
          title: const Text('确认删除?',
              style: TextStyle(
                  color: Colors.black87,
                  fontWeight: FontWeight.w500,
                  fontSize: 14)),
          // content: Text('$content'),
          actions: [
            CupertinoDialogAction(
              isDestructiveAction: true,
              isDefaultAction: false, // 设置分割线宽度较窄
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 13,
                    fontWeight: FontWeight.w500,
                  )),
            ),
            CupertinoDialogAction(
              isDefaultAction: false, // 设置分割线宽度较窄
              child: const Text(
                '确认',
                style: TextStyle(
                  color: Colors.blue,
                  fontSize: 13,
                  fontWeight: FontWeight.w500,
                ),
              ),
              onPressed: () {
                Navigator.of(context).pop();
                function?.call();
              },
            ),
          ],
        );
      },
    );
  }

  static Widget create(
      {double verticalPadding = 0,
      double horizontalPadding = 0,
      String msg = "暂无数据",
      double width = 50,
      double fontSize = 12,
      required String asset,
      color = const Color(0XFF656771)}) {
    return Center(
      child: Padding(
        padding: EdgeInsets.symmetric(
            vertical: verticalPadding, horizontal: horizontalPadding),
        child: Column(
          children: [
            Image.asset(
              asset,
              width: width,
            ),
            Text(
              "$msg",
              style: TextStyle(
                color: color,
                fontSize: fontSize,
              ),
            )
          ],
        ),
      ),
    );
  }


  static Widget meadeWidget(String? url, Function ? function,
      {required String asset3d, required String assetAudio}) {
    Widget view = const SizedBox();
    if (url == null || url.isEmpty) {
      return view;
    }
    if (url.endsWith("vrm") || url.endsWith("glb")) {
      view = ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(8.wpx)),
        child: Image.asset(asset3d),
      );
    } else if (url.endsWith("mp3") ||
        url.endsWith("wav") ||
        url.endsWith("wmv")) {
      view = ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(8.wpx)),
        child: Image.asset(assetAudio),
      );
    } else if (url.endsWith("mp4") ||
        url.endsWith("m2v") ||
        url.endsWith("mkv")) {
      view = ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(8.wpx)),
          child: VideoWidget(
            url: url ?? '',
            aspectRatio: 1.0,
            isMaxPlayer: true,
            function: function,
          ));
    } else {
      //图片
      view = ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(8.wpx)),
        child: CachedNetworkImage(
          // width: double.infinity,
          // height: double.infinity,
          fit: BoxFit.fill,
          imageUrl: url,
          errorWidget: (context, url, error) => Container(
            // width: double.infinity,
            // height: double.infinity,
            color: Colors.black12.withOpacity(125),
          ),
        ),
      );
    }
    return view;
  }
}
